package com.saascloud.redis.core;

import com.saascloud.core.saas.SaasContextUtil;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;

/**
 * @Author: liangxu
 * @Contact: one_dream@aliyun.com
 * @Date: 2022/5/13 16:34
 * @Version: 1.0
 * @Description:
 */
public class SaasRedisKeySerializer extends StringRedisSerializer {

    @Override
    public byte[] serialize(String string) {
        if (!TenantIgnore.getIgnore()) {
            String tenantId = SaasContextUtil.getTenantId();
            if (StringUtils.hasLength(tenantId)) {
                string = tenantId + ":" + string;
            }
        }
        return string == null ? null : string.getBytes(StandardCharsets.UTF_8);
    }

}
